﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace teach
{
    public partial class OperationPanel : PanelBase
    {
        public OperationPanel()
        {
            InitializeComponent();
        }

        public Button ButtonAnswer
        {
            get
            {
                return btnNext;
            }
        }

        private int count = 10;
        private bool timeouted = false;

        /// <summary>
        /// Set the operation question running
        /// </summary>
        /// <param name="question"></param>
        public override void SetQuestion(Question question)
        {
            Reset();
            m_answered = false;
            m_currentQuestion = question;
            IOperation operation = question as IOperation;

            switch (operation.EmptyField)
            {
                case 0:
                    txtFirst.Text = operation.First.ToString();
                    txtFirst.ReadOnly = true;
                    txtSecond.Text = operation.Second.ToString();
                    txtSecond.ReadOnly = true;
                    txtAnswer.Text = "";
                    txtAnswer.ReadOnly = false;
                    txtAnswer.Focus();
                    break;
                case 1:
                    txtFirst.Text = "";
                    txtFirst.ReadOnly = false;
                    txtSecond.Text = operation.Second.ToString();
                    txtSecond.ReadOnly = true;
                    txtAnswer.Text = operation.OperationAnswer.ToString();
                    txtAnswer.ReadOnly = true;
                    txtFirst.Focus();
                    break;
                case 2:
                    txtFirst.Text = operation.First.ToString();
                    txtFirst.ReadOnly = true;
                    txtSecond.Text = "";
                    txtSecond.ReadOnly = false;
                    txtAnswer.Text = operation.OperationAnswer.ToString();
                    txtAnswer.ReadOnly = true;
                    txtSecond.Focus();
                    break;
            }
            txtSign.Text = operation.Sign;
            this.timeoutMeter.Interval = 12000;
            this.timeoutMeter.Enabled = true;
        }

        public override Control GetAnswerControl()
        {
            Control result = null;
            IOperation operation = m_currentQuestion as IOperation;
            switch (operation.EmptyField)
            {
                case 0:
                    result = txtAnswer;
                    break;
                case 1:
                    result = txtFirst;
                    break;
                case 2:
                    result = txtSecond;
                    break;
            }
            return result;
        }

        public override void SetStatusImage(bool success, Image image)
        {
            pctStatus.Visible = true;
            pctStatus.Image = image;
            pctStatus.Refresh();
            IOperation operation = m_currentQuestion as IOperation;
            //
            if (success)
            {
                switch (operation.EmptyField)
                {
                    case 0:
                        txtAnswer.ReadOnly = true;
                        txtAnswer.BackColor = Color.LightBlue;
                        break;
                    case 1:
                        txtFirst.ReadOnly = true;
                        txtFirst.BackColor = Color.LightBlue;
                        break;
                    case 2:
                        txtSecond.ReadOnly = true;
                        txtSecond.BackColor = Color.LightBlue;
                        break;
                }
            }
            else
            {

                switch (operation.EmptyField)
                {
                    case 0:
                        txtAnswer.ReadOnly = true;
                        txtAnswer.BackColor = Color.Tomato;
                        txtAnswer.Text = m_currentQuestion.Answer.ToString();
                        break;
                    case 1:
                        txtFirst.ReadOnly = true;
                        txtFirst.BackColor = Color.Tomato;
                        txtFirst.Text = m_currentQuestion.Answer.ToString();
                        break;
                    case 2:
                        txtSecond.ReadOnly = true;
                        txtSecond.BackColor = Color.Tomato;
                        txtSecond.Text = m_currentQuestion.Answer.ToString();
                        break;
                }
            }
        }

        private void Reset()
        {
            timeouted = false;
            pctStatus.Visible = false;
            pctStatus.Image = null;
            pctStatus.Refresh();
            txtAnswer.ReadOnly = false;
            txtAnswer.ForeColor = Color.Black;
            txtAnswer.BackColor = Color.White;
            txtAnswer.Text = "";

            txtCounter.Visible = false;
            count = 10;

            txtFirst.ReadOnly = false;
            txtFirst.ForeColor = Color.Black;
            txtFirst.BackColor = Color.White;
            txtFirst.Text = "";

            txtSecond.ReadOnly = false;
            txtSecond.ForeColor = Color.Black;
            txtSecond.BackColor = Color.White;
            txtSecond.Text = "";
            btnNext.Text = "Réponse";
        }

        private void timeoutMeter_Tick(object sender, EventArgs e)
        {
            txtCounter.Text = count.ToString();
            timeoutMeter.Interval = 2000;
            txtCounter.Visible = true;
            if (count == 0)
            {
                timeouted = true;
                btnNext.PerformClick();
            }
            count--;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            IOperation operation = m_currentQuestion as IOperation;
            m_answered = !m_answered;
            timeoutMeter.Enabled = false;
            txtCounter.Visible = false;
            if (m_answered)
            {
                switch (operation.EmptyField)
                {
                    case 0:
                        if (txtAnswer.Text.Trim() == string.Empty && !timeouted)
                        {
                            m_answered = !m_answered;
                            return;
                        }
                        base.RaiseAnswer(txtAnswer.Text);
                        break;
                    case 1:
                        if (txtFirst.Text.Trim() == string.Empty)
                        {
                            m_answered = !m_answered;
                            return;
                        }
                        base.RaiseAnswer(txtFirst.Text);
                        break;
                    case 2:
                        if (txtSecond.Text.Trim() == string.Empty)
                        {
                            m_answered = !m_answered;
                            return;
                        }
                        base.RaiseAnswer(txtSecond.Text);
                        break;
                }
                
                btnNext.Text = "Prochain";
            }
            else
            {
                Reset();
                base.RaiseNextClicked();
            }
        }
    }
}
